#property copyright "Copyright 2011, Eric"
#property link      "mingxinhe@gmail.com"

#property indicator_chart_window
//----
extern string CurrencyList = "EURUSD,USDJPY,USDCAD,USDCHF,GBPUSD,AUDUSD,NZDUSD,EURCHF,EURGBP,EURNZD";
extern bool M5_Enable = true;
extern bool M15_Enable = false;
extern bool M30_Enable = true;
extern bool H1_Enable = true;
extern bool H4_Enable = true;
extern bool D1_Enable = false;
extern bool W1_Enable = false;

extern int WPRPeriod = 14;
extern int RSIPeriod = 14;
extern double UpperLevel.WPR = -20; 
extern double LowerLevel.WPR = -80; 
extern double UpperLevel.RSI = 60; 
extern double LowerLevel.RSI = 40; 
extern color Blue1ColourSignal.Color = Blue;
extern color Red1ColourSignal.Color = Red;
extern color Blue2ColourSignal.Color = Green;
extern color Red2ColourSignal.Color = Orchid;
extern color Text.Currency.Color = Maroon;
extern color Text.TimeFrame.Color = DeepSkyBlue;

extern bool Text.Alert = false;
extern bool Sound.Alert = false;
extern bool Email.Alert = false;
extern string Indicator_Name = "WPR_RSI_Alert";
extern string SoundFileName = "Alert.wav";

#define NUM_TF 7

int U=20,L=0;
int barCount[][NUM_TF];
string symbols[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{       
    SplitString(CurrencyList, ",", symbols);
    ArrayResize(barCount, ArraySize(symbols));
       
    int SY,I,Y=31+U,X=23+L,Y2=Y-19,X2=X+72,X3=X2+2,Y3=Y;SY=Y3;
    string tex=""; 
    for(int i=0;i<ArraySize(symbols);i++)
    {
         tex=""; tex=symbols[i];   
         ObjectCreate(tex+"EricHe",23,0,0,0);
         ObjectSet(tex+"EricHe",102,X2);ObjectSet(tex+"EricHe",103,Y2);X2+=80;
         ObjectSetText(tex+"EricHe",tex,10,"Arial",Text.Currency.Color);
    }
    
    if (M5_Enable)
    {
         tex= "M5EricHe";
         ObjectCreate(tex,23,0,0,0);
         ObjectSet(tex,OBJPROP_XDISTANCE,X);ObjectSet(tex,OBJPROP_YDISTANCE,Y);Y+=50;
         ObjectSetText(tex,"M5",12,"Arial",Text.TimeFrame.Color);
    }
    if (M15_Enable)
    {
         tex= "M15EricHe";
         ObjectCreate(tex,23,0,0,0);
         ObjectSet(tex,OBJPROP_XDISTANCE,X);ObjectSet(tex,OBJPROP_YDISTANCE,Y);Y+=50;
         ObjectSetText(tex,"M15",12,"Arial",Text.TimeFrame.Color);
    }
    if (M30_Enable)
    {
         tex= "M30EricHe";
         ObjectCreate(tex,23,0,0,0);
         ObjectSet(tex,OBJPROP_XDISTANCE,X);ObjectSet(tex,OBJPROP_YDISTANCE,Y);Y+=50;
         ObjectSetText(tex,"M30",12,"Arial",Text.TimeFrame.Color);
    }
    if (H1_Enable)
    {
         tex= "H1EricHe";
         ObjectCreate(tex,23,0,0,0);
         ObjectSet(tex,OBJPROP_XDISTANCE,X);ObjectSet(tex,OBJPROP_YDISTANCE,Y);Y+=50;
         ObjectSetText(tex,"H1",12,"Arial",Text.TimeFrame.Color);
    }
    if (H4_Enable)
    {
         tex= "H4EricHe";
         ObjectCreate(tex,23,0,0,0);
         ObjectSet(tex,OBJPROP_XDISTANCE,X);ObjectSet(tex,OBJPROP_YDISTANCE,Y);Y+=50;
         ObjectSetText(tex,"H4",12,"Arial",Text.TimeFrame.Color);
    }
    if (D1_Enable)
    {
         tex= "D1EricHe";
         ObjectCreate(tex,23,0,0,0);
         ObjectSet(tex,OBJPROP_XDISTANCE,X);ObjectSet(tex,OBJPROP_YDISTANCE,Y);Y+=50;
         ObjectSetText(tex,"D1",12,"Arial",Text.TimeFrame.Color);
    }
    if (W1_Enable)
    {
         tex= "W1EricHe";
         ObjectCreate(tex,23,0,0,0);
         ObjectSet(tex,OBJPROP_XDISTANCE,X);ObjectSet(tex,OBJPROP_YDISTANCE,Y);Y+=50;
         ObjectSetText(tex,"W1",12,"Arial",Text.TimeFrame.Color);
    }
   
   for(i=0;i<ArraySize(symbols);i++)
   {
       if (M5_Enable)
       {                                   
           createArrow(symbols[i]+"_M5_ColorBuy1", X3, Y3);
           createArrow(symbols[i]+"_M5_ColorSell1", X3 + 15, Y3);
           createArrow(symbols[i]+"_M5_ColorBuy2", X3 + 30, Y3);
           createArrow(symbols[i]+"_M5_ColorSell2", X3 + 45, Y3);                      
           Y3+=50;                                                                         	
       }
       if (M15_Enable)
       {
           createArrow(symbols[i]+"_M15_ColorBuy1", X3, Y3);
           createArrow(symbols[i]+"_M15_ColorSell1", X3 + 15, Y3);
           createArrow(symbols[i]+"_M15_ColorBuy2", X3 + 30, Y3);
           createArrow(symbols[i]+"_M15_ColorSell2", X3 + 45, Y3);
           Y3+=50;
       }
       if (M30_Enable)
       {
           createArrow(symbols[i]+"_M30_ColorBuy1", X3, Y3);
           createArrow(symbols[i]+"_M30_ColorSell1", X3 + 15, Y3);
           createArrow(symbols[i]+"_M30_ColorBuy2", X3 + 30, Y3);
           createArrow(symbols[i]+"_M30_ColorSell2", X3 + 45, Y3);
           Y3+=50;
       }
       if (H1_Enable)
       {
           createArrow(symbols[i]+"_H1_ColorBuy1", X3, Y3);
           createArrow(symbols[i]+"_H1_ColorSell1", X3 + 15, Y3);
           createArrow(symbols[i]+"_H1_ColorBuy2", X3 + 30, Y3);
           createArrow(symbols[i]+"_H1_ColorSell2", X3 + 45, Y3);
           Y3+=50;
       }
       if (H4_Enable)
       {
           createArrow(symbols[i]+"_H4_ColorBuy1", X3, Y3);
           createArrow(symbols[i]+"_H4_ColorSell1", X3 + 15, Y3);
           createArrow(symbols[i]+"_H4_ColorBuy2", X3 + 30, Y3);
           createArrow(symbols[i]+"_H4_ColorSell2", X3 + 45, Y3);
           Y3+=50;
       }
       if (D1_Enable)
       {
           createArrow(symbols[i]+"_D1_ColorBuy1", X3, Y3);
           createArrow(symbols[i]+"_D1_ColorSell1", X3 + 15, Y3);
           createArrow(symbols[i]+"_D1_ColorBuy2", X3 + 30, Y3);
           createArrow(symbols[i]+"_D1_ColorSell2", X3 + 45, Y3);
           Y3+=50;
       }
       if (W1_Enable)
       {
           createArrow(symbols[i]+"_W1_ColorBuy1", X3, Y3);
           createArrow(symbols[i]+"_W1_ColorSell1", X3 + 15, Y3);
           createArrow(symbols[i]+"_W1_ColorBuy2", X3 + 30, Y3);
           createArrow(symbols[i]+"_W1_ColorSell2", X3 + 45, Y3);
           Y3+=50;
       }

       X3+=80;Y3=SY;
   }
}

void createArrow(string name, double X, double Y)
{
      ObjectCreate(name,23,0,0,0);
      ObjectSet(name,OBJPROP_XDISTANCE,X);
      ObjectSet(name,OBJPROP_YDISTANCE,Y);                                 
      ObjectSetText(name,"",12,"Times New Roman",0); 
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
    ObjectsDeleteAll();
}

int start()
{
    for(int i=0;i<ArraySize(symbols);i++)
    {   
        if (M5_Enable)
        {            
            updateArrows(symbols[i], PERIOD_M5);    
        }        
        if (M15_Enable)
        {            
            updateArrows(symbols[i], PERIOD_M15);
        }
        if (M30_Enable)
        {            
            updateArrows(symbols[i], PERIOD_M30);
        }
        if (H1_Enable)
        {            
            updateArrows(symbols[i], PERIOD_H1);
        }
        if (H4_Enable)
        {            
            updateArrows(symbols[i], PERIOD_H4);
        }
        if (D1_Enable)
        {            
            updateArrows(symbols[i], PERIOD_D1);
        }
        if (W1_Enable)
        {            
            updateArrows(symbols[i], PERIOD_W1);
        }        
    }  
}

void updateArrows(string symbol, int timeframe)
{
      double Red1ColourSignal = iCustom(symbol, timeframe, Indicator_Name, WPRPeriod, RSIPeriod, UpperLevel.WPR, LowerLevel.WPR, UpperLevel.RSI,LowerLevel.RSI, false, false, false, false, 0, 0);            
      if (Red1ColourSignal != 0)
      {
          if (ObjectGet(symbol+"_"+ getTimeFrame(timeframe) + "_ColorSell1", OBJPROP_COLOR)  == 0)
          {
              doAlert(symbol, timeframe, "Red1 ColourSignal");   
          }       
          ObjectSetText(symbol+"_"+ getTimeFrame(timeframe) + "_ColorSell1", CharToStr( 234 ), 10,  "Wingdings", Red1ColourSignal.Color);
      }
      else
      {
          ObjectSetText(symbol+"_"+ getTimeFrame(timeframe) + "_ColorSell1","",12,"Times New Roman",0); 
      }
      double Blue1ColourSignal = iCustom(symbol, timeframe, Indicator_Name, WPRPeriod, RSIPeriod, UpperLevel.WPR, LowerLevel.WPR, UpperLevel.RSI,LowerLevel.RSI, false, false, false, false, 1, 0);            
      if (Blue1ColourSignal != 0)
      {
          if (ObjectGet(symbol+"_"+ getTimeFrame(timeframe) + "_ColorBuy1", OBJPROP_COLOR)  == 0)
          {
              doAlert(symbol, timeframe, "Blue1 ColourSignal");            
              ObjectSetText(symbol+"_"+ getTimeFrame(timeframe) + "_ColorBuy1", CharToStr( 233 ), 10,  "Wingdings", Blue1ColourSignal.Color);
          }
      }
      else
      {
          ObjectSetText(symbol+"_"+ getTimeFrame(timeframe) + "_ColorBuy1","",12,"Times New Roman",0); 
      }
      double Red2ColourSignal = iCustom(symbol, timeframe, Indicator_Name, WPRPeriod, RSIPeriod, UpperLevel.WPR, LowerLevel.WPR, UpperLevel.RSI,LowerLevel.RSI, false, false, false, false, 2, 0);            
      if (Red2ColourSignal != 0 && ObjectGet(symbol+"_"+ getTimeFrame(timeframe) + "_ColorSell2", OBJPROP_COLOR)  == 0)
      {
          doAlert(symbol, timeframe, "Red2 ColourSignal");            
          ObjectSetText(symbol+"_"+ getTimeFrame(timeframe) + "_ColorSell2", CharToStr( 234 ), 12,  "Wingdings", Red2ColourSignal.Color);
      }
      else
      {
          ObjectSetText(symbol+"_"+ getTimeFrame(timeframe) + "_ColorSell2","",12,"Times New Roman",0); 
      }
      double Blue2ColourSignal = iCustom(symbol, timeframe, Indicator_Name, WPRPeriod, RSIPeriod, UpperLevel.WPR, LowerLevel.WPR, UpperLevel.RSI,LowerLevel.RSI, false, false, false, false, 3, 0);            
      if (Blue2ColourSignal != 0 && ObjectGet(symbol+"_"+ getTimeFrame(timeframe) + "_ColorBuy2", OBJPROP_COLOR)  == 0)
      {
          doAlert(symbol, timeframe, "Blue2 ColourSignal");            
          ObjectSetText(symbol+"_"+ getTimeFrame(timeframe) + "_ColorBuy2", CharToStr( 233 ), 12,  "Wingdings", Blue2ColourSignal.Color);
      }
      else
      {
          ObjectSetText(symbol+"_"+ getTimeFrame(timeframe) + "_ColorBuy2","",12,"Times New Roman",0); 
      }
}

void doAlert(string symbol, int timeFrame, string SignalType)
{
      string alertStr = "Pair:" + symbol + ", T/F:" +  getTimeFrame(timeFrame) + ", SignalType:" + SignalType;                   
      if (Text.Alert)
      {
          Alert(alertStr);
          Print(alertStr);
      }
      if (Email.Alert)
      {
          SendMail("Candle Pattern Alert" , alertStr);  
      }
      if (Sound.Alert)
      {
          PlaySound(SoundFileName);
      }   
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
string getTimeFrame(int tf)
{
switch(tf)
 {
 case 1: return("M1");
 case 5: return("M5");
 case 15: return("M15");
 case 30: return("M30");
 case 60: return("H1");
 case 240: return("H4");
 case 1440: return("D1");
 case 10080: return("W1");
 case 43200: return("MN");
 }
}

// *************************************************************************************
// Service function for splitting a string into an array of values. For example,
// it can be used to take a comma-separated line (read using ReadLineFromFile() etc)
// and break it into an array of its comma-delimited parts.
// Example usage is:
//      string Results[];
//      StringSplit("abc,def,ghi", ",", Results);
// *************************************************************************************
bool SplitString(string stringValue, string separatorSymbol, string& results[], int expectedResultCount = 0)
{
   if (StringFind(stringValue, separatorSymbol) < 0)
   {// No separators found, the entire string is the result.
      ArrayResize(results, 1);
      results[0] = stringValue;
   }
   else
   {   
      int separatorPos = 0;
      int newSeparatorPos = 0;
      int size = 0;

      while(newSeparatorPos > -1)
      {
         size = size + 1;
         newSeparatorPos = StringFind(stringValue, separatorSymbol, separatorPos);
         
         ArrayResize(results, size);
         if (newSeparatorPos > -1)
         {
            if (newSeparatorPos - separatorPos > 0)
            {  // Evade filling empty positions, since 0 size is considered by the StringSubstr as entire string to the end.
               results[size-1] = StringSubstr(stringValue, separatorPos, newSeparatorPos - separatorPos);               
            }
         }
         else
         {  // Reached final element.
            results[size-1] = StringSubstr(stringValue, separatorPos, 0);
         }         
         results[size-1] = StringTrimLeft(StringTrimRight(results[size-1]));
         
         separatorPos = newSeparatorPos + 1;
      }
   }   
   
   if (expectedResultCount == 0 || expectedResultCount == ArraySize(results))
   {  // Results OK.
      return (true);
   }
   else
   {  // Results are WRONG.
      Print("ERROR - size of parsed string not expected.");
      return (false);
   }
}

